package org.algorithm.biubiu.wheel_1.链表;

import org.algorithm.biubiu.wheel_1.common.ListNode;
import org.junit.Test;

/**
 * @author jiazhaoxin
 * @Date 2022/2/15
 * @Desc
 */
public class 合并两个链表 {


    @Test
    public void test() {
        ListNode build = ListNode.build(0, 1, 2, 3, 4, 5);
        ListNode build2 = ListNode.build(1000, 10001);
        mergeInBetween(build, 3, 4, build2);

    }

    public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
        ListNode splitLeft = list1;
        for (int i = 1; i < a; i++) {
            splitLeft = splitLeft.next;
        }
        ListNode splitRight = splitLeft;
        for (int i = 0; i < (b - a + 2); i++) {
            splitRight = splitRight.next;
        }
        ListNode lastNode = getLastNode(list2);
        lastNode.next = splitRight;
        splitLeft.next = list2;
        return list1;

    }

    public ListNode getLastNode(ListNode node) {
        ListNode dummy = node;
        while (dummy.next != null) {
            dummy = dummy.next;
        }
        return dummy;
    }
}
